<?php

namespace App\Admin\Controllers\User;

use App\Models\SecurityUser;
use App\Models\User;
use App\Models\UserSecurity;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class SecurityController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '安保需求';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserSecurity());

        $grid->column('id', __('id'));
        $grid->column('user_id', '用户')->display(function ($id){
            $user = User::find($id);
            if ($user) {
                return $user->name;
            }
        });
        $grid->column('security_user_id', '安保用户')->display(function ($id){
            $user = SecurityUser::find($id);
            if ($user) {
                return $user->name;
            }
        });
        $grid->column('position', '申请地区')->latlong('latitude', 'longitude', $height = 400, $zoom = 16);
        $grid->column('status', '安保状态')->display(function ($i){
            $option = [1=>'寻求安保',2=>'安保中',3=>'安保结束'];
            return $option[$i];
        });
        $grid->column('create_time', '创建时间');
        $grid->column('update_time', '更新时间');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(UserSecurity::findOrFail($id));

        $show->field('id', __('id'));
        $show->field('user_id', '用户')->as(function ($id){
            $user = User::find($id);
            if ($user) {
                return $user->name;
            }
        });
        $show->field('security_user_id', '安保用户')->as(function ($id){
            $user = SecurityUser::find($id);
            if ($user) {
                return $user->name;
            }
        });
        $show->field('position','申请地区')->latlong('latitude', 'longitude', $height = 400, $zoom = 16);

        $show->field('status', '安保状态')->as(function ($status){
            return [1=>'寻求安保',2=>'安保中',3=>'安保结束'][$status];
        });
        $show->field('create_time', '创建时间');
        $show->field('update_time', '更新时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new UserSecurity());

        $form->select('user_id', '用户')->options(function ($id) {
            $user = User::find($id);
            if ($user) {
                return [$user->id => $user->name];
            }
        })->ajax('/admin/ajax/user');;
        $form->select('security_user_id', '安保用户')->options(function ($id) {
            $user = User::find($id);
            if ($user) {
                return [$user->id => $user->name];
            }
        })->ajax('/admin/ajax/security-user');
        $form->latlong('latitude', 'longitude', '坐标');
        $form->radio('status', '安保状态')->options([
            1=>'寻求安保',2=>'安保中',3=>'安保结束'
        ])->default(1);

        /*$form->number('create_time', '创建时间');
        $form->number('update_time', '更新时间');*/

        $form->hidden('position');

        $form->saving(function (Form $form) {
            $form->position = join(',', [$form->latitude, $form->longitude]);
        });


        return $form;
    }
}
